package com.yanqu.road.server.cmd.activity.snowgarden;

import com.yanqu.road.entity.activity.snowgarden.SnowGardenVisitUserData;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SnowGardenProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.List;

@Cmd(code = CrossProtocol.C_SNOW_GARDEN_VISIT_LIST, desc = "冰雪游园拜访列表")
public class SnowGardenVisitListCrossCmd extends CrossCommand {
    private long userId;
    private long serverId;
    private int page;
    private int pageSize;
    private List<SnowGardenProto.SnowGardenVisitUserDataMsg> visitData;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        SnowGardenProto.SnowGardenCrossVisitListReqMsg req = SnowGardenProto.SnowGardenCrossVisitListReqMsg.parseFrom(packet.getBytes());
        userId = req.getUserId();
        serverId = req.getUserServerId();
        visitData = req.getVisitUserListList();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        SnowGardenProto.SnowGardenVisitListRespMsg.Builder msg = SnowGardenProto.SnowGardenVisitListRespMsg.newBuilder();
        msg.setRet(0);
        for (SnowGardenProto.SnowGardenVisitUserDataMsg data:visitData){
            SnowGardenVisitUserData userData = new SnowGardenVisitUserData();
            long id = data.getUserId();
            UserBaseInfo base = CrossUserMgr.getCrossUserBaseInfo(id).getUserBaseInfo();
            userData.setUserId(id);
            userData.setUserData(base);
            userData.setScore(data.getScore());
            userData.setBuildingLevel(data.getBuildingLevel());
            userData.setHelpTimes(data.getHelpTimes());
            msg.addVisitUserList(parseSnowGardenVisitUserData(userData));
        }
        YanQuMessage message = YanQuMessageUtils.buildMessage(ClientProtocol.U_SNOW_GARDEN_GET_VISITOR_LIST, msg);
        MessageHelper.sendPacket(serverId, userId, message);
    }

    @Override
    public void sendData() {

    }

    private static SnowGardenProto.SnowGardenVisitUserDataMsg.Builder parseSnowGardenVisitUserData(SnowGardenVisitUserData data){
        SnowGardenProto.SnowGardenVisitUserDataMsg.Builder builder = SnowGardenProto.SnowGardenVisitUserDataMsg.newBuilder();
        builder.setUserId(data.getUserId());
        builder.setUserData(PlayerBasePb.parsePlayerBaseTempMsg(data.getUserData()));
        builder.setScore(data.getScore());
        builder.setBuildingLevel(data.getBuildingLevel());
        builder.setHelpTimes(data.getHelpTimes());
        return builder;
    }
}
